<?php

class ldapTask extends sfBaseTask
{
  protected function configure()
  {

    $this->namespace        = 'wixet';
    $this->name             = 'install-ldap';
    $this->briefDescription = 'Install ldap';
    
  }

  protected function execute($arguments = array(), $options = array())
  {
  	if(sfConfig::get('sf_ldap_admin') == ""){
			$settings = sfYaml::load(sfConfig::get('sf_root_dir')."/config/settings.yml");
			$admin = $settings['all']['ldap']['admin'];
			$adminPass = $settings['all']['ldap']['password'];
			$host = $settings['all']['ldap']['hostname'];
		}else{
			$admin = sfConfig::get('sf_ldap_admin');
			$adminPass = sfConfig::get('sf_ldap_password');
			$host = sfConfig::get('sf_ldap_hostname');
		}
		
		$ds = ldap_connect($host,10389);
		if(!$ds) throw new Exception('Cant connect to LDAP host "'.$host.'"');
		ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
		
		if(!ldap_bind($ds, $admin,  $adminPass)) throw new Exception('Cant bind to LDAP host "'.$host.'"');
		
		$info = array();
		$info['dc']="wixet";
		$info['objectClass']="organization";
		$info['o']="wixet";



	ldap_add($ds, "dc=wixet", $info);
	$info['ou']="People";
	$info['objectClass']="organizationalUnit";
    if(ldap_add($ds, $settings['all']['ldap']['suffix'], $info))
    	$this->log("LDAP database created");
    else
    	 throw new Exception('Cant create LDAP database (already exists?)');
  }
}
